package com.goatstone.client;

import java.util.StringTokenizer;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
//import com.goatstone.client.MainWidgetComposite.AboutDialog;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
//import com.google.gwt.dev.asm.Label;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

import org.gwt.beansbinding.core.client.BeanProperty;
import org.gwt.beansbinding.core.client.Binding;
import org.gwt.beansbinding.core.client.BindingGroup;
import org.gwt.beansbinding.core.client.Bindings;
import org.gwt.beansbinding.core.client.AutoBinding.UpdateStrategy;
import org.gwt.beansbinding.core.client.ext.BeanAdapterFactory;
import org.gwt.beansbinding.core.client.util.GWTBeansBinding;
import org.gwt.beansbinding.ui.client.adapters.TextBoxAdapterProvider;
import org.gwt.mosaic.ui.client.WindowPanel;


public class AlphabDraw implements EntryPoint{

	private User user; 
	private ServerServiceAsync serverService;

	// back-end communications
	private boolean cancelRepeatCall;
	private int timeOutCheckMili;	
	private boolean serverHasCalledBack;
	private MainWidgetComposite mwc;

	// messaging
//	String userModeMessage="";
//	String userInstructionsMessage="";	


	public void onModuleLoad() {
		user = new User(null);
		serverService = GWT.create(ServerService.class);
		mwc = new MainWidgetComposite( user, serverService );

		// start the repeat call here
		repeatCallToPushFunction();

		// repeatCallTimeoutTimer : timer checks to see if the the repeat call to server has returned in time
		// package these vars in class RepeatCallTimeoutTimer
		cancelRepeatCall = false;
		timeOutCheckMili = 1000 * 60 * 7;		// (1000 * 60 * n) n minutes
		//		timeOutCheckMili = 1000 * 1 ;		// (1000 * 60 * n) n minutes
		serverHasCalledBack = false;
		RepeatCallTimeoutTimer repeatCallTimeoutTimer = new RepeatCallTimeoutTimer();
		repeatCallTimeoutTimer.scheduleRepeating( timeOutCheckMili );

	}

	class RepeatCallTimeoutTimer extends Timer {
		int i = 0;
		@Override
		public void run() {
			mwc.setFeedBack( String.valueOf(i++) );
			if ( serverHasCalledBack == false){
				mwc.setFeedBack("Your session has expired, try reloading the browser");
				// cancel the repeatCallToPushFunction
				cancelRepeatCall = true;
			}
			serverHasCalledBack = false;
		}
	}

	public void repeatCallToPushFunction(){
		// create dataRequest object
		ClientRequestDataCheckServer  clientRequestDataCheckServer = new ClientRequestDataCheckServer();
		clientRequestDataCheckServer.latestClientElementTimeStamp = mwc.alphabCanvas.lastElementTimeStamp;
		clientRequestDataCheckServer.userId = user.getId();	
		// at this point the time starts,
		if(cancelRepeatCall == false){
			serverService.checkServer( clientRequestDataCheckServer, checkServerAsyncCallback);
		}
	}

	final AsyncCallback<ServerResponseData> checkServerAsyncCallback = new AsyncCallback<ServerResponseData>() {
		boolean repeatRunning = true;
		String message = ""; 
//		int i=0;
		public void onFailure(Throwable caught) {
			repeatCallToPushFunction();
		}
		public void onSuccess(ServerResponseData serverResponseData) {

			//	is there a assignedClientId? , assignment  zero is setting user on lurk mode
			if(serverResponseData.assignedClientId != null){
				//				message += "id set";
				user.setId( serverResponseData.assignedClientId );
				// serverResponseData.assignedClientId == 0 is the "lurk" state, user can not add elements
				if(serverResponseData.assignedClientId == 0){
					// take out the click handler from
					message += " There are "+serverResponseData.totalUsers + " users online. ";			
					message  += "The maximum number of users has been reached, you are on Lurk mode. ";

				}else{
					message += " There are "+serverResponseData.totalUsers + " users online. ";			
					message += "Type into the text box above and click the mouse onto the canvas area.";
					//					message += "add handler";
					mwc.alphabCanvas.addHandler();
				}
			}

			// check to see if the serverResponseData has a command
			if( serverResponseData.command != null){
				if( serverResponseData.command.equals("reset") ){
					// !!!!!!!! disable alphaCanvas
					message += "Reset called:"+serverResponseData.message;
					repeatRunning = false;
					user.setId(null);
				}
			}

			// add the missing elements, if they exist
			if(serverResponseData.alphaDrawMissingElementList != null ){
				for(int i=0; i<serverResponseData.alphaDrawMissingElementList.size(); i++ ){
					mwc.alphabCanvas.addDrawElement( serverResponseData.alphaDrawMissingElementList.get(i) );				
				}
				mwc.alphabCanvas.lastElementTimeStamp = serverResponseData.alphaDrawMissingElementList.get(
						(serverResponseData.alphaDrawMissingElementList.size()-1)		
				).unixServerTimeStamp;
			}

			mwc.setFeedBack(message);

			if (repeatRunning){ repeatCallToPushFunction(); }
		}
	};

}